package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Account;
import com.xiaochong.tir.common.data.po.Notice;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.filter.SessionManager;
import com.xiaochong.tir.manager.service.NoticeService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2018/6/1.
 */
@Controller
@RequestMapping(ManagerVersion.API_V1_PREFIX + "msg/")
@Api("通知管理")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 通知展示
     * @param model
     * @return
     */
    @GetMapping("mainpage")
    public String mainPage(Model model){
        Notice notice = new Notice();
        notice.setEnableFlag(true);
        List<Notice> list = noticeService.selectByEntityList(notice);
        if(list !=null){
            for (int i = 0; i <list.size() ; i++) {
                if(list.get(i).getEnableFlag().equals(false)){
                    list.remove(list.get(i));
                }
            }
        }
        model.addAttribute("nolist",list);
        return "main";
    }

    /**
     * 通知页面
     * @return
     */
    @GetMapping("noticepage")
    public String notPage(){
        return "/notice/notelist";
    }

    /**
     * 通知列表
     * @param notice
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @ApiOperation(value = "通知列表",notes = "通知列表")
    @PostMapping("noticelist")
    @ResponseBody
    public ReturnResult<Notice> getAllNotice(Notice notice,
                                             @RequestParam("limit") Integer pageSize,
                                             @RequestParam("page")Integer pageIndex){
        PageInfo<Notice> list = noticeService.selectByEntityList(notice,pageSize,pageIndex);
        ReturnResult<Notice> result = new ReturnResult<Notice>(0,"成功",list.getList(),list.getTotal());
        return result;
    }

    /**
     * 修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("noticeedit")
    public String editNotice(@RequestParam("id")Integer id,Model model){
        Notice notice = noticeService.selectByPrimaryKey(id);
        model.addAttribute("notice",notice);
        return "/notice/editNotice";
    }

    /**
     * 修改方法
     * @param notice
     * @param request
     * @return
     */
    @ApiOperation(value = "修改通知",notes = "修改通知")
    @PostMapping("editnotice")
    public String updateNotice(Notice notice, HttpServletRequest request){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改通知");
       notice.setUpdatetime(new Date());
        Account account = SessionManager.getLoginAccount(request.getSession());
        notice.setCreater(account.getUserName()+"/"+account.getNickName());
       int count =  noticeService.updateByPrimaryKey(notice,noticeService.selectByPrimaryKey(notice.getId()));
        return "/notice/notelist";
    }

    /**
     * 启用/停用
     * @param notice
     * @return
     */
    @ApiOperation(value = "修改通知状态",notes = "修改通知状态")
    @PostMapping("enablenotice")
    @ResponseBody
    public ReturnResult<Notice> enableNotice(Notice notice){
        redisServer.set(RedisKeysUtils.LOGINNAME,"通知启用/停用");
        notice.setUpdatetime(new Date());
        if(notice.getEnableFlag()){
            notice.setEnableFlag(false);
        }else {
            notice.setEnableFlag(true);
        }
        int count =  noticeService.updateByPrimaryKey(notice,noticeService.selectByPrimaryKey(notice.getId()));
        return new ReturnResult<Notice>(0,"成功",count,1);
    }

    /**
     * 新增页面
     * @return
     */
    @GetMapping("addnotice")
    public String addNotice(){
        return "/notice/addNotice";
    }

    /**
     * 新增方法
     * @param notice
     * @param request
     * @return
     */
    @ApiOperation(value = "新增通知状态",notes = "新增通知状态")
    @PostMapping("crenotice")
    public String creNotice(Notice notice, HttpServletRequest request){
        SessionUtil.setSessionAttribute(SessionUtil.LOGINNAME,"");
        redisServer.set(RedisKeysUtils.LOGINNAME,"通知新增");
        Notice n = noticeService.selectByEntity(notice);
        if(null != n){
            return "/notice/notelist";
        }
        notice.setCreatetime(new Date());
        Account account = SessionManager.getLoginAccount(request.getSession());
        notice.setCreater(account.getUserName()+"/"+account.getNickName());
        noticeService.insertSelective(notice);
        return "/notice/notelist";
    }
}
